﻿using EasyCore.UnifyResult.Results;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.UnifyResult.Filters
{
    /// <summary>
    /// 统一结果过淲器
    /// 对于返回结果不是IEasyApiResult的结果进行包装
    /// </summary>
    public class UnifyResultFilter : IAsyncResultFilter, IOrderedFilter
    {
        public int Order => 99999;

        /// <summary>
        /// 在结果执行前进行处理
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            if (!(context.Result is ViewResult || context.Result is PartialViewResult || context.Result is RedirectResult
                || context.Result is RedirectToActionResult || context.Result is FileResult)) {
                //如果方法返回的结果是空结果，包装成一个成功的结果
                if (context.Result is EmptyResult emptyResult)
                {
                    //返回200状态码
                    //请求成功，但故意返回空内容	
                    context.Result = new ObjectResult(EasyApiResult.Ok("操作成功"));
                }
                else if (context.Result is NoContentResult noContent)
                {
                    //当前端接收到204状态码会默认不处理
                    //请求成功，服务器已处理，但不需要返回任何内容
                    context.Result = new ObjectResult(EasyApiResult.Ok("操作成功"));
                }
                else if (context.Result is BadRequestResult badRequest)
                {
                    context.Result = new ObjectResult(EasyApiResult.Errors(badRequest.StatusCode, "客户端错误请求")); //返回400状态码
                }
                else if (context.Result is NotFoundObjectResult notFound)
                {
                    context.Result = new ObjectResult(EasyApiResult.Errors(notFound.StatusCode ?? 404, "资源未找到"));
                }
                else if (context.Result is ConflictObjectResult conflictObject)
                {
                    context.Result = new ObjectResult(EasyApiResult.Errors(conflictObject.StatusCode ?? 409, "资源冲突"));
                }
                else if (context.Result is UnauthorizedResult unauthorized)
                {
                    context.Result = new ObjectResult(EasyApiResult.Errors(unauthorized.StatusCode, "未授权请求")); //返回401状态码
                }
                else if (context.Result is ForbidResult forbid)
                {
                    context.Result = new ObjectResult(EasyApiResult.Errors(403, "禁止访问")); //返回403状态码
                }
                //对返回结果进行统一处理
                //如果返回结果不是IEasyApiResult的结果进行包装
                if (context.Result is ObjectResult objResult)
                {
                    if (!(objResult.Value is IEasyApiResult))
                    {
                        context.Result = new ObjectResult(new EasyApiResult<object>(objResult.Value));
                    }
                }
            }            
            await next();
        }
    }
}
